import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/dashboard/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/auth/login/LoginView.vue'),
      meta: {
        title: '登录 - 小橘自动化运维平台',
        requiresAuth: false
      }
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('@/views/auth/register/RegisterView.vue'),
      meta: {
        title: '注册 - 小橘自动化运维平台',
        requiresAuth: false
      }
    },
    {
      path: '/forget-password',
      name: 'forget-password',
      component: () => import('@/views/auth/forget-password/ForgetPassword.vue'),
      meta: {
        title: '找回密码 - 小橘自动化运维平台',
        requiresAuth: false
      }
    },
    {
      path: '/dashboard',
      name: 'dashboard',
      component: HomeView,
      meta: {
        title: '仪表板 - 小橘自动化运维平台',
        requiresAuth: true
      }
    },
    {
      path: '/about',
      name: 'about',
      component: () => import('../views/AboutView.vue'),
      meta: {
        title: '关于 - 小橘自动化运维平台',
        requiresAuth: true
      }
    },
  ],
})

// 路由守卫 - 检查认证状态
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta?.title) {
    document.title = to.meta.title as string
  }

  // 检查是否需要认证
  if (to.meta?.requiresAuth) {
    // 这里应该检查用户是否已登录
    // 暂时跳过认证检查，直接允许访问
    const isAuthenticated = false // 实际项目中应该从状态管理或本地存储中获取

    if (!isAuthenticated && !['login', 'register', 'forget-password'].includes(to.name as string)) {
      // 未认证用户重定向到登录页
      next({ name: 'login' })
      return
    }
  }

  next()
})

export default router
